package org.example;

import org.example.command.*;
import org.example.editor.Editor;
import org.example.log.SessionLog;

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Editor editor = new Editor();
        Scanner scanner = new Scanner(System.in);
        String[] command;
        SessionLog logs = new SessionLog();
        do {
            System.out.print("> ");
            String line = scanner.nextLine();
            command = line.split(" ",3);
            if (command[0].equalsIgnoreCase("exit")){
                System.out.println("End!");
                logs.write();
                for (int i = 0;i < editor.getTotalFileNumber();i++){
                    editor.executeCommand(new CloseFileCommand(editor,i+1,scanner));
                }
                editor.getSessionLog().writeTime();
                return;
            }
            logs.add(line);
            switch (command[0].toLowerCase()){
                case "help":{
                    System.out.println("Available commands:");
                    System.out.println("  load filePath     - Load a file");
                    System.out.println("  ws                - Display loaded filename");
                    System.out.println("  save              - Save changes to the file");
                    System.out.println("  close fileNumber  - Close the file");
                    System.out.println("  exit              - Exit the command processor");
                    System.out.println("  switch fileNumber - Switch to another file");
                    System.out.println("  list              - Display the content of file");
                    System.out.println("  list-tree         - Display the structure of file");
                    System.out.println("  undo              - Undo the last modification command");
                    System.out.println("  redo              - Redo the undone command");
                    System.out.println("  history [number]  - Display the command history");
                    System.out.println("  stats [all/current]           - Display the structure of file");
                    System.out.println("  insert [lineNumber] content   - Insert text at a specific position");
                    System.out.println("  delete content/lineNumber     - Delete text at a specific position");
                    System.out.println("  append-head content           - Append text to the beginning of the file");
                    System.out.println("  append-tail content           - Append text to the end of the file");

                    break;
                }
                case "load":{
                    if (command.length < 2){
                        System.out.println("Please enter the filename you want to load!");
                        break;
                    }
                    editor.executeCommand(new LoadFileCommand(editor,command[1]));
                    break;
                }
                case "save":{
                    Integer fileNumber = editor.getCurrentFileNumber() + 1;
                    editor.executeCommand(new SaveFileCommand(editor, fileNumber));
                    break;
                }
                case "close":{
                    if (command.length < 2){
                        System.out.println("Please enter the file number you want to close!");
                        break;
                    }
                    if (!command[1].matches("\\d+") || command[1].startsWith("0")){
                        System.out.println("Please enter a valid number!");
                        break;
                    }
//                    System.out.println(command[1]);
                    editor.executeCommand(new CloseFileCommand(editor,Integer.parseInt(command[1]),scanner));
                    break;
                }
                case "ws":{
                    editor.executeCommand(new WSCommand(editor));
                    break;
                }
                case "switch":{
                    if (command.length < 2){
                        System.out.println("Please enter the file number you want to switch!");
                        break;
                    }
                    if (!command[1].matches("\\d+") || command[1].startsWith("0")){
                        System.out.println("Please enter a valid number!");
                        break;
                    }
                    editor.executeCommand(new SwitchCommand(editor,Integer.parseInt(command[1])));
                    break;
                }
                case "insert":{
                    if (command.length < 2){
                        System.out.println("Please enter the correct command!");
                        break;
                    }
                    if (editor.getCurrentFilePath() == null){
                        System.out.println("Please load a file first!");
                        break;
                    }
                    if (command.length == 2){
                        editor.executeCommand(new InsertCommand(editor,-1,command[1]));
                        break;
                    }
                    if (!command[1].matches("\\d+") || command[1].startsWith("0")){
                        command = line.trim().split(" ",2);
                        editor.executeCommand(new InsertCommand(editor,-1,command[1]));
                        break;
                    }
                    editor.executeCommand(new InsertCommand(editor,Integer.valueOf(command[1]),command[2]));
                    break;
                }
                case "append-head":{
                    if (command.length < 2){
                        System.out.println("Please enter the content you want to insert!");
                        break;
                    }
                    command = line.trim().split(" ",2);
                    editor.executeCommand(new AppendHeadCommand(editor,command[1]));
                    break;
                }
                case "append-tail":{
                    if (command.length < 2){
                        System.out.println("Please enter the content you want to insert!");
                        break;
                    }
                    command = line.trim().split(" ",2);
                    editor.executeCommand(new AppendTailCommand(editor,command[1]));
                    break;
                }
                case "delete":{
                    if (command.length < 2){
                        System.out.println("Please enter the content you want to delete!");
                        break;
                    }
                    editor.executeCommand(new DeleteCommand(editor,command[1]));
                    break;
                }
                case "list":{
                    editor.executeCommand(new ListCommand(editor));
                    break;
                }
                case "list-tree":{
                    editor.executeCommand(new ListTreeCommand(editor,"#不可能存在的输入"));
                    break;
                }
                case "undo":{
                    editor.executeCommand(new UndoCommand(editor));
                    break;
                }
                case "redo":{
                    editor.executeCommand(new RedoCommand(editor));
                    break;
                }
                case "history":{
                    if (command.length < 2){
                        editor.executeCommand(new HistoryCommand(editor,-1));
                        break;
                    }
                    if (!command[1].matches("\\d+") || command[1].startsWith("0")){
                        System.out.println("Please enter a valid number!");
                        break;
                    }
                    editor.executeCommand(new HistoryCommand(editor,Long.parseLong(command[1])));
                    break;
                }
                case "stats":{
                    if (command.length == 2 && command[1].equals("all")){
                        editor.executeCommand(new StatsCommand(editor,"all"));
                        break;
                    }
                    editor.executeCommand(new StatsCommand(editor,"current"));
                    break;
                }
                case "dir-tree":{
                    if (command.length < 2){
                        editor.executeCommand(new ListTreeCommand(editor,"#不可能存在的输入"));
                        break;
                    }
                    command = line.trim().split(" ",2);
                    editor.executeCommand(new ListTreeCommand(editor,command[1]));
                    break;
                }
                default:{
                    System.out.println("Unknown command. Type 'help' for a list of commands.");
                }
            }
        }while (!command[0].equalsIgnoreCase("exit"));
        scanner.close();
    }
}